from Parents.Base.Base import Base
from LBT.Resource.WelcomeAtt import WelcomeAtt
from LBT.Resource.LoginAtt import LoginAtt
from LBT.GeneralOperation.LoginOperation import loginClass
from Parents.Tools.mytools import tools
from LBT.GeneralOperation.ModuleOperation import moduleOper
from Parents.Functions.GeneralFunction import GeneralFunction
from selenium.webdriver.support.ui import WebDriverWait


class lbtlogin(Base, WelcomeAtt, LoginAtt, GeneralFunction, tools):
    '''
     所有lbt用例继承的父类，进行了封装登录的操作
     '''

    def login(self):
        '''
        所有乐贝通默认登录的操作
        :return:
        '''
        login = self.openUrl()
        login.loginClick()
        login.choiceSchool()
        self.moduleOp = moduleOper(self.driver)

    def openUrl(self):
        '''
        打开网页链接
        :return:
        '''
        print("准备开始打开网址")
        login = loginClass(self.driver)
        login.lbt_open(login.urlForLbtLogin)
        return login

    def catchfun(fun):
        """
        捕获异常，平台用例的装饰器
        :return:
        """

        def trueFun(*args, **kwargs):
            isError = 'FinallyPng_'
            args[0].docstr = "你为什么没写注释？！！！"
            try:
                fun(args[0])
            except AssertionError as ae:
                isError = 'AssertErrorPng_'
                tools().writeErrorLog(str(ae))
                raise AssertionError(ae)
            except Exception as e:
                isError = 'ErrorPng_'
                tools().writeErrorLog(str(e))
                raise Exception(e)
            finally:
                errorpng = isError + tools().getCurrentTime() + ".png"
                args[0].screenshotpng=tools().insert_img(args[0].driver, 'LBT', errorpng, __file__)
        return trueFun

    def loginFun(fun):
        """
        捕获异常并登录成功--装饰器
        :return:
        """
        def trueFun(*args, **kwargs):
            isError = 'FinallyPng_'
            args[0].docstr="你为什么没写注释？！！！"
            args[0].login()
            try:
                fun(args[0])
            except AssertionError as ae:
                isError = 'AssertErrorPng_'
                tools().writeErrorLog(str(ae))
                raise AssertionError(ae)
            except Exception as e:
                isError = 'ErrorPng_'
                tools().writeErrorLog(str(e))
                raise Exception(e)
            finally:
                errorpng = isError + tools().getCurrentTime() + ".png"
                args[0].screenshotpng=tools().insert_img(args[0].driver, 'LBT', errorpng, __file__)
        return trueFun
